<?php
require_once ('classes/models/event.php');
require_once ('classes/models/user.php');
require_once ('classes/managers/event_manager.php');

class EventController extends Controller
{
	public function edit()
	{
		set_global_default('id', 0);
		$content = new View('views/event/edit.php');
		
		$content->event = new Event($_GET['id']);
		
		$this->template->title = "Edit Event";
		$this->template->content = $content;
		echo $this->template;
	}
	
	public function detail()
	{
		set_global_default('id', 0);
		$content = new View('views/event/detail.php');
		
		$content->event = new Event($_GET['id']);
		
		$this->template->title = "Event Detail";
		$this->template->content = $content;
		echo $this->template;
	}
	
	public function save()
	{
		$event = new Event($_POST['id']);
		$event->set_values();
		$event->save();
		redirect('index.php');
	}
	
	public function month_grid()
	{
		set_global_default('month', date('m'));
		set_global_default('year', date('Y'));
		
		$month_boundaries=$this->get_month_boundaries($_GET['month'], $_GET['year']);
		$manager = new EventManager;
		$manager->from=$month_boundaries['first'];
		$manager->to=$month_boundaries['last'];
		
		$events=$manager->get();
		$events=$this->sort_events_by_date($events);
		
		$content = new View('views/event/month_grid.php');
		$content->events=$events;
		$content->from=$month_boundaries['first'];
		$content->to=$month_boundaries['last'];
		
		$prev_month=$this->clone_date($month_boundaries['first']);
		$prev_month->modify('-1 months');

		$next_month=$this->clone_date($month_boundaries['first']);
		$next_month->modify('+1 months');
		
		$content->prev_month=$prev_month;
		$content->next_month=$next_month;
		
		$this->template->subtitle = $month_boundaries['first']->format('F Y');
		
		$this->template->content=$content;
		
		echo $this->template;
	}
	
	public function view_list()
	{
		set_global_default('user_id', current_user()->id);
		$user = new User($_GET['user_id']);
		$events = $user->get_events();
		
		$content = new View('views/event/list.php');
		$content->events=$events;
		$this->template->content = $content;
		echo $this->template;
	}
	
	public function delete()
	{
		set_global_default('id', 0);
		$event = new Event($_POST['id']);
		$event->delete();
		
		redirect('index.php');
	}
	
	private function get_month_boundaries($month, $year)
	{
		$return=array();
		$first = new DateTime($year.'-'.$month.'-'."1");
		$last = new DateTime($year.'-'.$month.'-'."1");
		$last->modify('+1 months');
		$last->modify('-1 days');
		$return['first'] = $first;
		$return['last'] = $last;
		
		return $return;
	}
	
	private function sort_events_by_date($events)
	{
		$return = array();
		foreach($events as $event)
		{
			$event_date = $event->event_date->format('Y-m-d');
			if(!array_key_exists($event_date, $return))
			{
				$return[$event_date] = array();
			}
			$return[$event_date][] = $event;
		}
		return $return;
	}
	
	private function clone_date($date)
	{
		return new DateTime($date->format('Y-m-d'));
	}

}

?>